class Image < ActiveRecord::Base

  attr_protected []

  belongs_to :user_id

  has_attached_file :file, 
    :styles => { 
      :large => "800x800>", 
      :normal => "300x300>",
      :small => '64x64>' 
    },
    :hash_data => ':id/:style/:updated_at/:basename.:extension',
    :hash_secret => "longSecretString",
    :url => "/uploaded/images/:id_partition/:hash.:extension",
    :path => ":rails_root/public/uploaded/images/:id_partition/:hash.:extension" 

  validates_attachment_content_type :file,
    :content_type =>[
      'image/pjpeg',
      'image/jpeg',
      'image/x-png'
    ]
  validates_attachment_size :file, 
    :less_than => 2.megabytes
  
  validates_attachment_presence :file

  after_create do |image|
    image.project.bg_image = image
    image.project.save(:validates => false)
  end

  def url(style = 'original') 
    file.url(style)
  end
end
